package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.Neumatico;
import negModelExcep.*;

public class NeumaticoTest extends TestCase {

	Neumatico neum1,neum2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		neum1 = new Neumatico(100,20,150,new String ("neum"),new AlgoPesos(100),false,10,100,10,3);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","neumatico");
		
		e.setAttribute("maxVU", ""+ neum1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ neum1.getVidaUtil());
		e.setAttribute("peso", ""+ neum1.getPeso());
		e.setAttribute("aportevmax", ""+ neum1.aportevmax());
		e.setAttribute("detalle", neum1.getDetalle());
		e.setAttribute("precio", ""+(neum1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+neum1.esRemovible());
		e.setAttribute("performance", ""+neum1.getPerformance());
		
		e.setAttribute("perimetro", ""+neum1.getPerimetro());
		e.setAttribute("ancho", ""+neum1.getAncho());
		e.setAttribute("coeficiente", ""+neum1.getCoeficiente());
		
	}

	public void testGastar() {
		try {
			neum1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(99,neum1.getVidaUtil(),0.0);
	}

	public void testFuncionar() {
		try {
			assertEquals(0.3,neum1.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
	}

	public void testGetPerimetro() {
		assertEquals(100,neum1.getPerimetro(),0.0);
		
	}

	public void testGetAncho() {
		assertEquals(10,neum1.getAncho(),0.0);
		
	}

	public void testGetCoeficiente() {
		assertEquals(3,neum1.getCoeficiente(),0.0);
		
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=neum1.toXml(doc);
		
		assertEquals(neum1.getPerimetro(),Double.parseDouble(e.getAttribute("perimetro")),0.0);
		assertEquals(neum1.getAncho(),Double.parseDouble(e.getAttribute("ancho")),0.0);
		assertEquals(neum1.getCoeficiente(),Double.parseDouble(e.getAttribute("coeficiente")),0.0);
		
		assertEquals(neum1.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((neum1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(neum1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(neum1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(neum1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(neum1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testNeumatico ()throws Exception {
		
		neum2=new Neumatico(e);
				
		assertEquals(neum2.getPerimetro(),Double.parseDouble(e.getAttribute("perimetro")),0.0);
		assertEquals(neum2.getAncho(),Double.parseDouble(e.getAttribute("ancho")),0.0);
		assertEquals(neum2.getCoeficiente(),Double.parseDouble(e.getAttribute("coeficiente")),0.0);
		
		assertEquals(neum2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((neum2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(neum2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(neum2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(neum2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(neum2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}

}
